﻿//   Copyright 2009 Pyxis Technologies
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//

using NUnit.Framework;
using Pyxis.FluentSelenium.Locator;
using Pyxis.FluentSelenium.Tester;

namespace Pyxis.FluentSelenium.Test
{
    [TestFixture]
    public class ContentTesterTest
    {
        [Test]
        public void TestShouldSeeTextFindsMatchingTextSuccess()
        {
            var selenium = new MockSelenium { StringResponses = "hello"};
            var contentTester = new ContentTester(selenium, "a");

            contentTester.ShouldSee("hello");

            Assert.AreEqual("GetText: a", selenium.Events);
        }

        [Test]
        public void TestShouldSeeTextFindsDifferentTextThrowsException()
        {
            var selenium = new MockSelenium { StringResponses = "hello" };
            var contentTester = new ContentTester(selenium, "b");
            try
            {
                contentTester.ShouldSee("goodbye");
                Assert.Fail();
            }
            catch (SeleniumAssertionException)
            {
                Assert.AreEqual("GetText: b", selenium.Events);
            }
        }

        [Test]
        public void TestShouldSeeChildElementForVisibleElementSuccess()
        {
            var selenium = new MockSelenium { BooleanResponses = true };
            var contentTester = new ContentTester(selenium, "c");
            var welcomeTextRegion = ElementLocator.WithId("welcome");

            contentTester.ShouldSee(welcomeTextRegion);

            Assert.AreEqual("IsVisible: c//*[@id='welcome']", selenium.Events);
        }

        [Test]
        public void TestShouldSeeChildElementForNonVisibleElementThrowsException()
        {
            var selenium = new MockSelenium { BooleanResponses = false };
            var contentTester = new ContentTester(selenium, "d");
            var welcomeTextRegion = ElementLocator.WithId("welcome");

            try
            {
                contentTester.ShouldSee(welcomeTextRegion);
                Assert.Fail();
            }
            catch (SeleniumAssertionException)
            {
                Assert.AreEqual("IsVisible: d//*[@id='welcome']", selenium.Events);
            }
        }

        [Test]
        public void TestShouldSeeChildElementWithTextSucceeds()
        {
            var selenium = new MockSelenium { StringResponses = "hello" };
            var contentTester = new ContentTester(selenium, "z");
            var welcomeTextRegion = ElementLocator.WithId("welcome");

            contentTester.ShouldSee(welcomeTextRegion, "hello");

            Assert.AreEqual("GetText: z//*[@id='welcome']", selenium.Events);
        }

        [Test]
        public void TestShouldSeeChildElementWithTextFindsDifferentTextThrowsException()
        {
            var selenium = new MockSelenium { StringResponses = "goodbye" };
            var contentTester = new ContentTester(selenium, "z");
            var welcomeTextRegion = ElementLocator.WithId("welcome");

            try
            {
                contentTester.ShouldSee(welcomeTextRegion, "hello");
                Assert.Fail();
            }
            catch (SeleniumAssertionException)
            {
                Assert.AreEqual("GetText: z//*[@id='welcome']", selenium.Events);
            }
        }


        [Test]
        public void TestShouldNotSeeChildElementForMissingSuccess()
        {
            var selenium = new MockSelenium { BooleanResponses = false };
            var contentTester = new ContentTester(selenium, "d");
            var welcomeTextRegion = ElementLocator.WithId("welcome");

            contentTester.ShouldNotSee(welcomeTextRegion);

            Assert.AreEqual("IsVisible: d//*[@id='welcome']", selenium.Events);
        }

        [Test]
        public void TestShouldNotSeeChildElementForVisibleElementThrowsException()
        {
            var selenium = new MockSelenium { BooleanResponses = true };
            var contentTester = new ContentTester(selenium, "x");
            var welcomeTextRegion = ElementLocator.WithId("welcome");

            try
            {
                contentTester.ShouldNotSee(welcomeTextRegion);
                Assert.Fail();
            }
            catch (SeleniumAssertionException)
            {
                Assert.AreEqual("IsVisible: x//*[@id='welcome']", selenium.Events);
            }
        }
    }
}
